package rs.frenjoynet.core.ui;

import org.jboss.seam.annotations.*;
import org.jboss.seam.ScopeType;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.frenjoynet.core.ui.BaseBean;

import rs.frenjoynet.core.core.ActiveUserSession;
import rs.frenjoynet.core.core.LoginKind;


/**
 * Created by IntelliJ IDEA.
 * User: ppopovic
 * Date: 08.10.2009.
 * Time: 11.09.37
 * To change this template use File | Settings | File Templates.
 */
@Name ("loginBean")
@Scope (ScopeType.EVENT)
@Install (false)
public class LoginBean extends BaseBean
{
	@In
	private Credentials credentials;

	@Logger
	private Log log;

	private boolean remeberMe;

	public boolean login()
	{
		log.debug("login");
		boolean result = false;

		try
		{
			String username = credentials.getUsername();
			String password = credentials.getPassword();
			String sessionId = ((HttpSession)getExternalContext().getSession(true)).getId();
			String remoteAddr = ((HttpServletRequest)getExternalContext().getRequest()).getRemoteAddr();
			String userAgent = getExternalContext().getRequestHeaderMap().get("User-Agent");

			log.debug("Login " + username + " - " + password + "-" + sessionId);
			ActiveUserSession authResult = null;
			try
			{
				authResult = getCoreSecurityFacade().autentificate(credentials.getUsername(), credentials.getPassword(),
						((HttpSession)getExternalContext().getSession(true)).getId(), userAgent,
						((HttpServletRequest)getExternalContext().getRequest()).getRemoteAddr());
				if (authResult != null)
				{
					result = true;
					if (authResult.getLoginKind() == LoginKind.Master_Administrator)
						Identity.instance().addRole("master_admin");
					else if (authResult.getLoginKind() == LoginKind.Customer)
					{
						Identity.instance().addRole("customer");
					}
					else if (authResult.getLoginKind() == LoginKind.Suport_Administrator)
					{
						Identity.instance().addRole("suport_admin");
					}
					setLoginKind(authResult.getLoginKind());
				}

				log.debug("authResult: " + authResult);
			}
			catch (Exception e)
			{
				log.debug(e.getLocalizedMessage());
			}


//			if (username.equals("admin") && password.equals("adminadmin"))
//				result = true;
//			else
//				result = false;
		}
		catch (Exception e)
		{
			log.debug(e.getMessage());
			//error("auth_block", handleError(e));
		}

		return result;
	}

	public void logout()
	{
		Identity.instance().logout();
//		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
//		session.invalidate();
	}

	public void checkGuidAuthentication()
	{
		checkGuidAuthentication((HttpServletRequest)getExternalContext().getRequest(),
				(HttpServletResponse)getExternalContext().getResponse());
	}

	public void checkGuidAuthentication(HttpServletRequest req, HttpServletResponse res)
	{
		log.debug("Chectk autentification");
	}

	public boolean isRemeberMe()
	{
		return remeberMe;
	}

	public void setRemeberMe(boolean remeberMe)
	{
		this.remeberMe = remeberMe;
	}

	public boolean isCanSee()
	{
		return getLogedPage().equals("/admin.xhtml");
	}
}
